package com.cskaoyan.gateway.controller.shopping;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.dto.*;
import com.mall.user.annotation.Anoymous;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.intercepter.TokenIntercepter;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: Silence_W
 * @date: 2021-01-23 15:45
 **/
@Slf4j
@RestController
@RequestMapping("/shopping")
public class CartsController {

    @Reference(timeout = 3000,check = false,retries = 0)
    ICartService iCartService;

    //查询当前用户的购物车
    @GetMapping("/carts")
    public ResponseData cartList(HttpServletRequest request){

        String userName = (String) request.getAttribute(TokenIntercepter.USER_INFO_KEY);
        JSONObject jsonObject = JSON.parseObject(userName);
         Long uid=Long.parseLong(jsonObject.get("uid").toString());
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(uid);
        CartListByIdResponse response = iCartService.getCartListById(cartListByIdRequest);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return  new ResponseUtil().setData(response.getCartProductDtos());

        }
        return  new ResponseUtil().setErrorMsg(response.getMsg());
    }

    //增加商品到购物车当中
    @PostMapping("/carts")
    public ResponseData addCarts(@RequestBody AddCartRequest addCartRequest){

        AddCartResponse addCartResponse = iCartService.addToCart(addCartRequest);
        if (addCartResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){

            return  new ResponseUtil().setData(addCartResponse.getMsg());

        }
        return  new ResponseUtil().setErrorMsg(addCartResponse.getMsg());


    }

    //跟新购物车商品
    @PutMapping("/carts")
    public ResponseData updateCarts(@RequestBody UpdateCartNumRequest updateCartNumRequest){

        UpdateCartNumResponse updateCartNumResponse = iCartService.updateCartNum(updateCartNumRequest);

        if (updateCartNumResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){

            return  new ResponseUtil().setData(updateCartNumResponse.getMsg());
        }

            return  new ResponseUtil().setErrorMsg(updateCartNumResponse.getMsg());
    }

    // 全选/全不选 购物车里的商品
    @PutMapping("/items")
    public ResponseData checkAllCartItem(@RequestBody CheckAllItemRequest request){
        CheckAllItemResponse response = iCartService.checkAllCartItem(request);
        if (response.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return  new ResponseUtil().setData(response.getMsg());
        }
        return  new ResponseUtil().setErrorMsg(Integer.parseInt(response.getCode()),response.getMsg());
    }

    //删除指定的购物车中的商品
    @DeleteMapping("/carts/{uid}/{pid}")
    public ResponseData deleteCart(@PathVariable("uid") Long uid,@PathVariable("pid") Long pid){
        DeleteCartItemRequest deleteCartItemRequest = new DeleteCartItemRequest();
        deleteCartItemRequest.setUserId(uid);
        deleteCartItemRequest.setItemId(pid);
        DeleteCartItemResponse deleteCartItemResponse = iCartService.deleteCartItem(deleteCartItemRequest);
        if (deleteCartItemResponse.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(deleteCartItemResponse.getMsg());
        }
            return new ResponseUtil().setErrorMsg(deleteCartItemResponse.getMsg());

    }

    //删除用户选中的购物车中的商品
    @DeleteMapping("/items/{uid}")
    public ResponseData deleteCart(@PathVariable("uid") Long uid){
        DeleteCheckedItemRequest deleteCheckedItemRequest = new DeleteCheckedItemRequest();
        deleteCheckedItemRequest.setUserId(uid);
        DeleteCheckedItemResposne deleteCheckedItemResposne = iCartService.deleteCheckedItem(deleteCheckedItemRequest);
        if (deleteCheckedItemResposne.getCode().equals(ShoppingRetCode.SUCCESS.getCode())){
            return new ResponseUtil().setData(deleteCheckedItemResposne.getMsg());
        }
        return new ResponseUtil().setErrorMsg(deleteCheckedItemResposne.getMsg());

    }


}
